package cn.bgs.serviceedu.controller;


import cn.bgs.serviceedu.entity.Doctor;
import cn.bgs.serviceedu.service.DoctorService;
import cn.bgs.serviceedu.service.HospitalService;
import cn.bgs.serviceedu.vo.DoctorQuery;
import cn.bgs.serviceedu.vo.RedisList;
import cn.bgs.serviceutils.utils.CSException;
import cn.bgs.serviceutils.utils.R;
import cn.bgs.serviceutils.utils.RedisUtils;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医生表 前端控制器
 * </p>
 *
 * @author YiChen
 * @since 2021-10-20
 */
@Api(description = "医生表")
@RestController
@RequestMapping("/serviceedu/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private HospitalService hospitalService;



    @ApiOperation(value = "分页模糊查询")
    @PostMapping("/findAll")
    @CachePut(key = "'DoctorList'",value = "DoctorList")
    public R findAll(@RequestBody DoctorQuery doctorQuery){
        QueryWrapper<Doctor> wrapper = Wrappers.<Doctor>query()
                .like(StringUtils.isNoneBlank(doctorQuery.getDoctorName()),"d.doctor_name",StringUtils.trimToEmpty(doctorQuery.getDoctorName()))
                .like(StringUtils.isNoneBlank(doctorQuery.getHospitalId()),"d.hospital_id",StringUtils.trimToEmpty(doctorQuery.getHospitalId()))
                .like(doctorQuery.getDoctorVariety()!=null && doctorQuery.getDoctorVariety()>=0,"d.doctor_variety",doctorQuery.getDoctorVariety())
                .eq("d.delete_flag",0)
                .orderByAsc("d.create_time");
        System.out.println(doctorService.listDoc(wrapper));
        return R.ok().data("list",doctorService.listDoc(wrapper));
    }

    /**
     * 查询所有医院
     * @return
     */
    @GetMapping("/listHosp")
    public R listHosp(){
        return R.ok().data("list",hospitalService.list());
    }


    /**
     * 根据ID删除
     * @param doctorId
     * @return
     */
    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("/del/{doctorId}")
    @CachePut(key = "'DoctorList'",value = "DoctorList")
    public R del(@PathVariable Integer doctorId){
        boolean b = false;
        try {
            b = doctorService.removeById(doctorId);
        } catch (Exception e) {
            throw new CSException(20001,"删除失败");
        }
        if (b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }


    /**
     * 根据ID单查
     * @param doctorId
     * @return
     */
    @ApiOperation(value = "根据ID单查")
    @GetMapping("/findById/{doctorId}")
    public R findById(@PathVariable Integer doctorId){
        return   R.ok().data("list",doctorService.getById(doctorId));
    }


    /**
     * 添加用户
     * @param doctor
     * @return
     */
    @ApiOperation(value = "添加医生")
    @PostMapping("/addDoctor")
    @CachePut(key = "'DoctorList'",value = "DoctorList")
    public R addUser(@RequestBody Doctor doctor ){
        //MD5密码加密
        MD5 md5 = new MD5();
        String s = md5.digestHex(doctor.getPassWord());
        doctor.setPassWord(s);
        doctor.setBrowse(0);
        boolean save = false;
        try {
            save = doctorService.save(doctor);
        } catch (Exception e) {
            throw new CSException(20001,"添加失败");
        }
        if (save){
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }


    /**
     * 修改用户
     * @param doctor
     * @return
     */
    @ApiOperation(value = "修改医生")
    @PutMapping("/update")
    @CachePut(key = "'DoctorList'",value = "DoctorList")
    public R update(@RequestBody Doctor doctor){
        boolean update = false;
        try {
            update = doctorService.updateById(doctor);
        } catch (Exception e) {
            throw new CSException(20001,"修改失败");
        }
        if (update){
            return  R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }


    /**
     * 查看医生
     * @param doctorId
     * @return
     */
    @ApiOperation(value = "查看医生")
    @GetMapping("/browse/{doctorId}")
    public R browse(@PathVariable Long doctorId){
        String key ="doctor:"+doctorId;
        long incr = redisUtils.incr(key, 1);
       return R.ok().data("count",incr);
    }

    /**
     * 查询所有医生浏览
     */
    @ApiOperation(value = "查询所有医生浏览")
    @GetMapping("/look")
    public R look(){
        List<RedisList> redisList = doctorService.getRedisList();
        return R.ok().data("list",redisList);
    }

    /**
     * 查询所有医生排行榜
     */
    @ApiOperation(value = "查询所有医生排行榜")
    @GetMapping("/top")
    @Cacheable(key = "'DoctorTop'",value = "DoctorTop")
    public R top(){
        List<Doctor> redisList = doctorService.getListTop();
        return R.ok().data("list",redisList);
    }

    /**
     * 手动持久化数据
     * @return
     */
    @ApiOperation(value = "手动持久化数据")
    @GetMapping("/add")
    public R add(){
        List<RedisList> redisList = doctorService.getRedisList();
        Doctor doctor = new Doctor();
        try {
            for (int i = 0; i < redisList.size(); i++) {
                RedisList redisList1 = redisList.get(i);
                Doctor byId = doctorService.getById(redisList1.getDoctorId());
                if (!byId.getBrowse().equals(redisList1.getValue())) {
                    doctor.setDoctorId(Integer.valueOf(redisList1.getDoctorId()));
                    doctor.setBrowse(redisList1.getValue());
                    doctorService.updateById(doctor);
                }
            }
        } catch (NumberFormatException e) {
            throw new CSException(20001,"修改失败");
        }
        return R.ok();
    }

    /**
     * 根据用户id查询所有收藏的医生
     * @param customerId
     * @return
     */
    @ApiOperation(value = "根据用户id查询所有收藏的医生")
    @GetMapping("/findDoct/{customerId}")
    public R findDoct(@PathVariable Integer customerId ){
        List<Doctor> doct= doctorService.findDoct(customerId);
        return R.ok().data("list",doct);
    }


}
